package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.po.MarketBrand;
import com.cskaoyan.app.bean.po.MarketBrandExample;
import com.cskaoyan.app.bean.po.MarketCategory;
import com.cskaoyan.app.bean.po.MarketCategoryExample;
import com.cskaoyan.app.bean.vo.CreategoodsCatAndBrandVo;
import com.cskaoyan.app.mapper.MarketBrandMapper;
import com.cskaoyan.app.mapper.MarketCategoryMapper;
import com.cskaoyan.app.service.AdminGoodsCatAndBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;


@Service
public class AdminGoodsCatAndBrandServiceImpl implements AdminGoodsCatAndBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;


    @Override
    public List<CreategoodsCatAndBrandVo.CategoryListEntity> getListCategoryListEntity() {

        List<CreategoodsCatAndBrandVo.CategoryListEntity> CategoryList = new LinkedList<>();
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(0);
        List<MarketCategory> categoryFatherList = marketCategoryMapper.selectByExample(example);
        for (MarketCategory category : categoryFatherList) {


            Integer pid = category.getId();
            String name = category.getName();
            MarketCategoryExample example1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andDeletedEqualTo(false);
            criteria1.andPidEqualTo(pid);
            List<MarketCategory> categoryChildList = marketCategoryMapper.selectByExample(example1);

            LinkedList<CreategoodsCatAndBrandVo.CategoryListEntity.ChildrenEntity> childrenList = new LinkedList<>();
            for (MarketCategory marketCategory : categoryChildList) {
                CreategoodsCatAndBrandVo.CategoryListEntity.ChildrenEntity child = new CreategoodsCatAndBrandVo.CategoryListEntity.ChildrenEntity();
                String name1 = marketCategory.getName();
                Integer id = marketCategory.getId();
                child.setValue(id);
                child.setLabel(name1);
                childrenList.add(child);

            }
            CreategoodsCatAndBrandVo.CategoryListEntity categoryListEntity = new CreategoodsCatAndBrandVo.CategoryListEntity();
            categoryListEntity.setValue(pid);
            categoryListEntity.setLabel(name);
            categoryListEntity.setChildren(childrenList);

            CategoryList.add(categoryListEntity);
        }



        return CategoryList;
    }

    @Override
    public List<CreategoodsCatAndBrandVo.BrandListEntity> getListBrandListEntity() {
        List<CreategoodsCatAndBrandVo.BrandListEntity> brandListEntityList = new LinkedList<>();
        MarketBrandExample example = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(example);
        for (MarketBrand marketBrand : marketBrands) {
            Integer id = marketBrand.getId();
            String name = marketBrand.getName();
            CreategoodsCatAndBrandVo.BrandListEntity brandListEntity = new CreategoodsCatAndBrandVo.BrandListEntity(name, id);
            brandListEntityList.add(brandListEntity);
        }
        return brandListEntityList;
    }
}
